package com.TP.www.service.impl;


import com.TP.www.dao.impl.CustomerDAO;
import com.TP.www.service.Register;
import com.TP.www.util.Md5;

import static com.TP.www.constant.Constant.*;

/**
 * @author 邱彦祥
 * 用户注册功能实现类
 */
public class CustomerRegister implements Register {
    CustomerDAO customerDAO = new CustomerDAO();
    /**
     * 用户注册
     */
    @Override
    public void register(){
        String name;
        String password;
        System.out.println("************用户注册*************");
        System.out.println("请输入用户名(字母开头，允许字母数字下划线):");
        name = SYS_SC.nextLine();
        while (!name.matches(NAME_REGULAR)) {
            System.out.println("用户名格式有误!");
            System.out.println("请重新输入用户名(字母开头，允许字母数字下划线):");
            name = SYS_SC.nextLine();
        }
        while(Register.check(name)){
            System.out.println("用户名很受欢迎哝!");
            System.out.println("请重新输入用户名(字母开头，允许字母数字下划线):");
            name = SYS_SC.nextLine();
        }
        System.out.println("请输入密码(以字母开头，长度在6~18之间，只能包含字母、数字和下划线):");
        password = SYS_SC.nextLine();
        while (!password.matches(PASSWORD_REGULAR)) {
            System.out.println("密码格式有误!");
            System.out.println("请重新输入密码(以字母开头，长度在6~18之间，只能包含字母、数字和下划线):");
            password = SYS_SC.nextLine();
        }
        String sql = "insert into customer(name, password, asset) values (?,?,0)";
        Md5 md = new Md5();
        String secretPassword = md.start(password).toUpperCase();
        if(customerDAO.executeUpdate(sql,name,secretPassword)>0){
            System.out.println("注册成功!");
        }
    }
}
